package com.demostic.demostic_test.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkRequest;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.demostic.demostic_test.R;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.utils.AlertDialogUtils;
import com.demostic.demostic_test.utils.Constants;
import com.demostic.demostic_test.utils.DialogUtils;
import com.demostic.demostic_test.utils.DisplayUtil;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.ToastUtils;
import com.demostic.demostic_test.utils.UserUtils;
import com.demostic.demostic_test.utils.WifiUtils;

import java.math.BigInteger;

public class SecondApActivity extends BaseActivity implements View.OnClickListener {
    public static ConnectivityManager.NetworkCallback callback;
    public static ConnectivityManager cm;
    final int CONNECT_ROBOT_WIFI = 25;
    final int REQUEST_CODE_LOCATION = 24;
    final String TAG = SecondApActivity.class.getSimpleName();
    AlertDialog alertDialog;
    String ap_ssid;
    private String bssid;
    Button bt_connect;
    Context context;
    String homeWifi;
    ImageView image_back;
    private boolean isFirst;
    boolean locationEnable;
    LocationManager locationManager;
    private final ContentObserver mGpsMonitor = new ContentObserver(null) {
        /* class com.demostic.demostic_test.activity.SecondApActivity.AnonymousClass1 */

        @Override // android.database.ContentObserver
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            SecondApActivity.this.locationEnable = SecondApActivity.this.locationManager.isProviderEnabled("gps");
            MyLog.e(SecondApActivity.this.TAG, "ContentObserver==:" + SecondApActivity.this.locationEnable);
        }
    };
    String pass;
    String physicalId;
    String subdomain;
    private TextView textView7;
    private Dialog tipsDialog;
    TextView tv_set;
    TextView tv_ssid;
    WifiManager wifiManager;

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ap_second);
        getSubdomain();
        initView();
        initData();
        showTipsDialog();
    }

    private void showTipsDialog() {
        View view_secondap = LayoutInflater.from(this.context).inflate(R.layout.secondap_dialog, (ViewGroup) null);
        this.tipsDialog = DialogUtils.showUnifyDialog(this.context, view_secondap);
        this.tipsDialog.setCanceledOnTouchOutside(false);
        this.tipsDialog.show();
        TextView tv_tips_set = (TextView) view_secondap.findViewById(R.id.tv_dialog_confirm);
        tv_tips_set.setText(getString(R.string.wifi_aty_go_setting));
        ((TextView) view_secondap.findViewById(R.id.tv_dialog_cancel)).setOnClickListener(this);
        tv_tips_set.setOnClickListener(this);
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onStart() {
        super.onStart();
        getContentResolver().registerContentObserver(Settings.Secure.getUriFor("location_providers_allowed"), false, this.mGpsMonitor);
    }

    public void getSubdomain() {
        Bundle bundle;
        Intent intent = getIntent();
        if (intent != null && (bundle = intent.getExtras()) != null) {
            this.subdomain = bundle.getString("subdomain");
        }
    }

    private void initData() {
        this.wifiManager = (WifiManager) this.context.getApplicationContext().getSystemService("wifi");
        this.locationManager = (LocationManager) this.context.getApplicationContext().getSystemService("location");
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            this.homeWifi = bundle.getString(FirstApActivity.EXTRA_SSID);
            this.pass = bundle.getString(FirstApActivity.EXTRA_PASS);
        }
        this.tv_ssid.setText(this.homeWifi);
    }

    private void initView() {
        this.context = this;
        this.isFirst = true;
        this.tv_set = (TextView) findViewById(R.id.tv_set);
        this.tv_ssid = (TextView) findViewById(R.id.tv_ssid);
        this.bt_connect = (Button) findViewById(R.id.bt_next);
        this.image_back = (ImageView) findViewById(R.id.image_back);
        this.textView7 = (TextView) findViewById(R.id.tv_step7);
        if (this.subdomain.equals(Constants.subdomain_x782) || this.subdomain.equals(Constants.subdomain_x785) || this.subdomain.equals(Constants.subdomain_x786) || this.subdomain.equals(Constants.subdomain_x787)) {
            this.textView7.setText(getString(R.string.ap_aty_five_step));
        } else {
            this.textView7.setText(getString(R.string.ap_aty_fourth_step));
        }
        this.tv_set.setOnClickListener(this);
        this.bt_connect.setOnClickListener(this);
        this.image_back.setOnClickListener(this);
    }

    @Override // android.app.Activity, android.view.Window.Callback
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            this.tv_ssid.setText(WifiUtils.getSsid(this.context));
        }
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_back /* 2131755189 */:
                unRegisterLocation();
                finish();
                return;
            case R.id.tv_set /* 2131755200 */:
                toSetting();
                return;
            case R.id.bt_next /* 2131755203 */:
                this.ap_ssid = this.tv_ssid.getText().toString().trim();
                WifiInfo info = this.wifiManager.getConnectionInfo();
                if (info != null) {
                    String bssid2 = info.getBSSID();
                    MyLog.e(this.TAG, "start connect==:" + bssid2);
                    if (TextUtils.isEmpty(bssid2)) {
                        return;
                    }
                    if (bssid2.startsWith("02")) {
                        showLocationDialog();
                        return;
                    } else {
                        connect(bssid2);
                        return;
                    }
                } else {
                    return;
                }
            case R.id.tv_dialog_cancel /* 2131755504 */:
                this.tipsDialog.dismiss();
                return;
            case R.id.tv_dialog_confirm /* 2131755505 */:
                toSetting();
                return;
            default:
                return;
        }
    }

    private void toSetting() {
        Intent intent = new Intent();
        intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
        startActivityForResult(intent, 25);
        this.tipsDialog.dismiss();
    }

    public boolean isTheSameDevice(String ap_ssid2, String physicalId2) {
        if (!TextUtils.isEmpty(ap_ssid2) && !TextUtils.isEmpty(physicalId2) && ap_ssid2.length() > 4 && physicalId2.length() > 4) {
            String ap_ssid_ = UserUtils.exChange(ap_ssid2);
            String physicalId_ = UserUtils.exChange(physicalId2);
            if (ap_ssid_.substring(ap_ssid_.length() - 4).equals(physicalId_.substring(physicalId_.length() - 4))) {
                return true;
            }
        }
        return false;
    }

    public void connect(String bssid2) {
        long mac;
        BigInteger id = new BigInteger(bssid2.replace(":", ""), 16);
        if (!bssid2.startsWith("84")) {
            mac = id.longValue() - 1;
        } else {
            mac = id.longValue();
        }
        this.physicalId = Long.toHexString(mac);
        if (TextUtils.isEmpty(this.ap_ssid) || !this.ap_ssid.startsWith("Robot") || TextUtils.isEmpty(this.physicalId) || !isTheSameDevice(this.ap_ssid, this.physicalId)) {
            showTipsDialog();
            ToastUtils.showToast(this.context, getString(R.string.ssid_startwith));
            return;
        }
        String localIp = getLocalIp();
        MyLog.e(this.TAG, "toConnect second==:" + this.homeWifi + "," + this.pass);
        Intent intent = new Intent(this, ThirdApActivity.class);
        intent.putExtra("subdomain", this.subdomain);
        intent.putExtra("physicalId", this.physicalId);
        intent.putExtra("bssid", bssid2);
        intent.putExtra("homeWifi", this.homeWifi);
        intent.putExtra("pass", this.pass);
        intent.putExtra("localIp", localIp);
        startActivity(intent);
    }

    public String getLocalIp() {
        return intToIp(this.wifiManager.getConnectionInfo().getIpAddress());
    }

    private String intToIp(int i) {
        return (i & 255) + "." + ((i >> 8) & 255) + "." + ((i >> 16) & 255) + "." + ((i >> 24) & 255);
    }

    public void showLocationDialog() {
        View v = LayoutInflater.from(this.context).inflate(R.layout.layout_open_location_dialog, (ViewGroup) null);
        v.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.activity.SecondApActivity.AnonymousClass2 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                AlertDialogUtils.hidden(SecondApActivity.this.alertDialog);
            }
        });
        v.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.activity.SecondApActivity.AnonymousClass3 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                AlertDialogUtils.hidden(SecondApActivity.this.alertDialog);
                SecondApActivity.this.startActivityForResult(new Intent("android.settings.LOCATION_SOURCE_SETTINGS"), 24);
            }
        });
        this.alertDialog = AlertDialogUtils.showDialog(this.context, v, DisplayUtil.dip2px(this.context, 300.0f), DisplayUtil.dip2px(this.context, 140.0f));
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyLog.e(this.TAG, "test Thread onActivityResult:" + Thread.currentThread().getId());
        this.tv_ssid.setText(WifiUtils.getSsid(this.context));
        this.ap_ssid = this.tv_ssid.getText().toString();
        WifiInfo info = this.wifiManager.getConnectionInfo();
        if (info != null) {
            this.bssid = info.getBSSID();
            if (TextUtils.isEmpty(this.bssid)) {
                return;
            }
        }
        cm = (ConnectivityManager) this.context.getSystemService("connectivity");
        if (cm == null) {
            return;
        }
        if (cm.getActiveNetworkInfo().getType() != 0) {
            toConnectDevices(requestCode);
        } else if (Build.VERSION.SDK_INT >= 21) {
            NetworkRequest.Builder builder = new NetworkRequest.Builder();
            builder.addTransportType(1);
            callback = new ConnectivityManager.NetworkCallback() {
                /* class com.demostic.demostic_test.activity.SecondApActivity.AnonymousClass4 */

                @Override // android.net.ConnectivityManager.NetworkCallback
                public void onAvailable(Network network) {
                    super.onAvailable(network);
                    MyLog.e(SecondApActivity.this.TAG, "test Thread onAvailable:" + Thread.currentThread().getId());
                    if (Build.VERSION.SDK_INT >= 23 && SecondApActivity.cm.bindProcessToNetwork(network) && SecondApActivity.this.isFirst) {
                        SecondApActivity.this.toConnectDevices(requestCode);
                        SecondApActivity.this.isFirst = false;
                    }
                }
            };
            cm.requestNetwork(builder.build(), callback);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void toConnectDevices(int requestCode) {
        if (requestCode == 25) {
            if (this.bssid.startsWith("02")) {
                showLocationDialog();
            } else {
                connect(this.bssid);
            }
        } else if (requestCode == 24) {
            this.locationEnable = this.locationManager.isProviderEnabled("gps");
            if (this.locationEnable) {
                connect(this.bssid);
            } else {
                ToastUtils.showToast(this.context, getString(R.string.third_ap_aty_location));
            }
        }
    }

    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onBackPressed() {
        unRegisterLocation();
        super.onBackPressed();
    }

    public void unRegisterLocation() {
        getContentResolver().unregisterContentObserver(this.mGpsMonitor);
    }
}
